import ajax from '@/api/ajax'
import { BACKEND_URL } from '@/config/config';
function handleParams(params) {
    // 删除空参数
    Object.keys(params).forEach(key => {
        if (params[key] === null || params[key] === undefined || params[key] === '') {
            delete params[key]
        }
    });
    // 解析日期
    let dates = params.createTime
    delete params.createTime
    if (dates && dates.length == 2) {
        params['cStartTime'] = dates[0]
        params['cEndTime'] = dates[1]
    }
    return params;
}
export default {
    data() {
        return {
            pagination: {
                limit: 15, // 每页数据大小
                pageNum: 1, // 当前页数
                total: 0 // 全部数据条数
            },
            orderList: []
        }
    },
    methods: {
        searchOrder(params) {
            let url = `/api/sys/orders`
            handleParams(params);
            return new Promise(resolve => {
                ajax.get(url, { params }).then(resp => {
                    let result = resp.data
                    if (result.status === 200) {
                        result = result.data;
                        this.pagination.total = result.total;
                        this.orderList = result.results;
                        resolve(result.data)
                    } else {
                        console.error('获取订单列表失败！');
                    }
                })
            })

        },
        handleCurrentChange(currentPage) {
            this.pagination.pageNum = currentPage;
            let params = Object.assign({}, this.pagination, this.searchForm);
            this.searchOrder(params)
        },
        handleSizeChange(size) {
            this.pagination.limit = size
            let params = Object.assign({}, this.pagination, this.searchForm);
            this.searchOrder(params)
        },
        // 到处到Excel
        exportExcel() {
            let params = Object.assign({}, this.pagination, this.searchForm);
            handleParams(params);
            let url = `${BACKEND_URL}/api/export/excel/orders?`;
            let keys = Object.keys(params);
            keys.forEach(key => {
                url += `${key}=${params[key]}&`
            });
            window.open(url);
        },
    }
}